package com.pactera.madp.cp.controller.oms.daily;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.daily.DailySendDTO;
import com.pactera.madp.cp.api.entity.daily.WorkDailySendEntity;
import com.pactera.madp.cp.api.vo.daily.DailyIndexVo;
import com.pactera.madp.cp.api.vo.daily.DailySendVo;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.DailyService;
import com.pactera.madp.cp.service.IWorkDailySendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/subscribe")
@Api(value = "subscribe", tags = "日报订阅内容")
public class SubscribeController {

    @Autowired
    private DailyService dailyService;
    @Autowired
    IWorkDailySendService workDailySendService;

    @SysLog("日报-发送内容")
    @PostMapping("/sendWayUpdate")
    @ApiModelProperty(value = "发送内容")
    public R sendWay(@RequestBody DailySendDTO dto) {
        return dailyService.sendContext(dto);
    }

    @SysLog("日报-是否订阅设置")
    @GetMapping("/isOrder")
    @ApiModelProperty(value = "是否订阅")
    public R isOrder(boolean isOrder) {
        Integer corpId = CorpContextHolder.getCorpId();
        UpdateWrapper<WorkDailySendEntity> wrapper = new UpdateWrapper<WorkDailySendEntity>();
        wrapper.eq("corp_id", corpId);
        wrapper.set("is_order", isOrder);
        boolean b = workDailySendService.update(null, wrapper);
        return b? R.ok("修改成功") : R.failed("修改失败");
    }

    @GetMapping("/query")
    @ApiModelProperty(value = "查询订阅信息")
    public R query() {
        DailySendVo vo = workDailySendService.querySend();
        return R.ok(vo);
    }
    @GetMapping("/index")
    @ApiModelProperty(value = "首页")
    public R index(Integer type) {
        Integer corpId = CorpContextHolder.getCorpId1();
        DailyIndexVo vo;
        if(corpId == null) {
            vo = new DailyIndexVo();
            vo.setDates(new ArrayList<>());
            return R.ok(vo);
        }
        vo = dailyService.dailyIndex(type);
        if (vo.getDates() == null || vo.getDates().isEmpty()) {
            vo.setDates(new ArrayList<>());
        }
        return R.ok(vo);
    }
    @GetMapping("/indexDo")
    @ApiOperation(value = "首页代办")
    public R indexDo(Page page) {
        IPage iPage = dailyService.indexDo(page);
        return R.ok(iPage);
    }

}
